package com.example.imitation_webchat_demo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.controller.AddressHandler;
import com.example.imitation_webchat_demo.entity.T_userInfo;

// 通讯录对应的适配器，是个可扩展适配器，实现Filterable接口
public class AddressListAdapter extends BaseExpandableListAdapter implements Filterable{
	private Context context;
	private List<String> groupList;
	private List<List<T_userInfo>> childList;
	private MyFilter myFilter;
	private List<String> copyGroupList;
	private List<List<T_userInfo>> copyChildList;
	
	private AddressHandler handler;
	
	public AddressListAdapter(Context context, List<String> groupList, List<List<T_userInfo>> childList){
		this.context = context;
		this.groupList = groupList;
		this.childList = childList;
		copyGroupList = groupList;
		copyChildList = childList;
	}

	@Override
	public int getGroupCount() {
		return groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupPosition;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupItemViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.address_group_item, null);
			TextView charView = (TextView) convertView.findViewById(R.id.address_group_item_char);
			holder = new GroupItemViewHolder(charView);
			convertView.setTag(holder);
			convertView.setClickable(true);
		}else{
			holder = (GroupItemViewHolder) convertView.getTag();
		}
		holder.charView.setText(groupList.get(groupPosition));
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildItemViewHolder viewHolder;
		T_userInfo  item = childList.get(groupPosition).get(childPosition);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.address_child_group_item, null);
			ImageView friendImage = (ImageView) convertView
					.findViewById(R.id.friend_image);
			TextView nameView = (TextView) convertView
					.findViewById(R.id.friend_name);
			viewHolder = new ChildItemViewHolder(friendImage, nameView);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ChildItemViewHolder) convertView.getTag();
		}
		viewHolder.friendImage.setImageResource(R.drawable.ic_launcher);
		viewHolder.nameView.setText(item.getUser_remark());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	private class GroupItemViewHolder {
		TextView charView;

		public GroupItemViewHolder(TextView charView) {
			this.charView = charView;
		}
	}
	
	private class ChildItemViewHolder{
		ImageView friendImage;
		TextView nameView;
		
		public ChildItemViewHolder(ImageView friendImage, TextView nameView) {
			this.friendImage = friendImage;
			this.nameView = nameView;
		}
	}

	@Override
	public Filter getFilter() {
		if (myFilter == null) {
			myFilter = new MyFilter();
		}
		return myFilter;
	}
	
	// 自定义过滤器
	private class MyFilter extends Filter{
		private List<String> filterGroupList;
		private List<List<T_userInfo>> filterChildList;
		private List<T_userInfo> filterChildListItem;
		private boolean isEmpty;
		
		@Override
		protected FilterResults performFiltering(CharSequence sequence) {
			filterGroupList = new ArrayList<String>();
			filterChildList = new ArrayList<List<T_userInfo>>();
			String filterString = sequence.toString();
			boolean flag;
			if(filterString == null || filterString.length() == 0){
				isEmpty = true;
				filterGroupList = copyGroupList;
				filterChildList = copyChildList;
			}else{
				isEmpty = false;
				for(int i = 0; i < copyChildList.size(); i++){
					flag = false;
					for(int j = 0; j < copyChildList.get(i).size(); j++){
						if(copyChildList.get(i).get(j).getUser_remark().contains(filterString)){
							if(!filterGroupList.contains(copyGroupList.get(i))){
								filterGroupList.add(copyGroupList.get(i));
								filterChildListItem = new ArrayList<T_userInfo>();
							}
							filterChildListItem.add(copyChildList.get(i).get(j));
							flag = true;
						}
					}
					if(flag == true){
						filterChildList.add(filterChildListItem);
					}
				}
			}
			return null;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			groupList = filterGroupList;
			childList = filterChildList;
			notifyDataSetChanged();
			
			handler.onReceive(isEmpty);
			
			// 展开所有
			for (int i = 0, length = getGroupCount(); i < length; i++) {
				AddressListAdapter.this.onGroupExpanded(i);
			}
		}
	}

	public AddressHandler getHandler() {
		return handler;
	}

	public void setHandler(AddressHandler handler) {
		this.handler = handler;
	}
}